package com.binary.coder.gift.core.interfaces.Invitation.assembler;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.binary.coder.gift.core.common.bean.PageBean;
import com.binary.coder.gift.core.domain.invitation.model.aggregate.HandleInvitationRecordAggregate;
import com.binary.coder.gift.core.domain.invitation.model.aggregate.InvitationCardAggregate;
import com.binary.coder.gift.core.domain.invitation.model.entity.AppInvitationCardEntity;
import com.binary.coder.gift.core.domain.invitation.model.entity.DesignElementEntity;
import com.binary.coder.gift.core.interfaces.Invitation.dto.req.SaveAppInvitationCardReq;
import com.binary.coder.gift.core.interfaces.Invitation.dto.req.SaveInvitationCardReq;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.QueryDesignElementRes;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.QueryHandleInvitationRecordRes;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.QueryInvitationCardRes;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atao
 * @version 1.0.0
 * @ClassName InvitationCardAssembler.java
 * @Description 装配请帖相关类
 * @createTime 2024年06月24日 08:58:00
 */
public class InvitationCardAssembler {

    public static PageBean<QueryInvitationCardRes> assemblerQueryInvitationCardRes(PageBean<InvitationCardAggregate> source) {
        PageBean<QueryInvitationCardRes> target = new PageBean<>(source.getCurrent(),source.getSize(), source.getTotal());
        target.setRecords(source.getRecords().stream().map(InvitationCardAssembler::assemblerQueryInvitationCardRes).collect(Collectors.toList()));
        return target;
    }

    public static QueryInvitationCardRes assemblerQueryInvitationCardRes(InvitationCardAggregate source) {
        return QueryInvitationCardRes.builder()
                .id(source.getInvitationCardId())
                .title(source.getTitle())
                .userId(source.getUserId())
                .designElementRes(assemblerQueryDesignElementRes(source.getDesignElementEntities()))
                .build();
    }

    public static List<QueryDesignElementRes> assemblerQueryDesignElementRes(List<DesignElementEntity> source) {
        return source.stream().map(InvitationCardAssembler::assemblerQueryDesignElementRes).collect(Collectors.toList());
    }

    public static QueryDesignElementRes assemblerQueryDesignElementRes(DesignElementEntity source) {
        return QueryDesignElementRes.builder()
                .id(source.getId())
                .content(source.getContent())
                .filePath(source.getFilePath())
                .height(source.getHeight())
                .width(source.getWidth())
                .positionX(source.getPositionX())
                .positionY(source.getPositionY())
                .typeCode(source.getTypeCode())
                .build();
    }

    public static AppInvitationCardEntity assemblerAppInvitationCardEntity(SaveAppInvitationCardReq source,Object userId) {
        AppInvitationCardEntity appInvitationCardEntity = AppInvitationCardEntity.builder()
                .id(source.getId())
                .userId((String) userId)
                .title(source.getTitle())
                .date(source.getDate())
                .time(source.getTime())
                .invitationType(source.getInvitationType())
                .location(source.getLocation())
                .description(source.getDescription())
                .build();
        return appInvitationCardEntity;
    }


    public static InvitationCardAggregate assemblerInvitationCardAggregate(SaveInvitationCardReq source) {
        InvitationCardAggregate invitationCardAggregate = InvitationCardAggregate.builder()
                .invitationCardId(source.getId())
                .title(source.getTitle())
                .userId(source.getUserId())
                .build();
        invitationCardAggregate.setDesignElementEntities(BeanUtil.copyToList(source.getDesignElementReqList(),DesignElementEntity.class));
        return invitationCardAggregate;
    }

    public static QueryHandleInvitationRecordRes assemblerQueryHandleInvitationRecordRes(HandleInvitationRecordAggregate source) {
        QueryHandleInvitationRecordRes queryHandleInvitationRecordRes = BeanUtil.copyProperties(source, QueryHandleInvitationRecordRes.class);
        QueryInvitationCardRes queryInvitationCardRes = InvitationCardAssembler.assemblerQueryInvitationCardRes(source.getInvitationCardAggregate());
        queryHandleInvitationRecordRes.setQueryInvitationCardRes(queryInvitationCardRes);
        return queryHandleInvitationRecordRes;
    }


    public static PageBean<QueryHandleInvitationRecordRes> assemblerQueryHandleInvitationRecordRes(PageBean<HandleInvitationRecordAggregate> source) {
        PageBean<QueryHandleInvitationRecordRes> res = new PageBean<>(source.getCurrent(),source.getSize(), source.getTotal());
        res.setRecords(source.getRecords().stream()
        .map(e->{
            QueryHandleInvitationRecordRes queryHandleInvitationRecordRes = BeanUtil.copyProperties(e, QueryHandleInvitationRecordRes.class);
            QueryInvitationCardRes queryInvitationCardRes = InvitationCardAssembler.assemblerQueryInvitationCardRes(e.getInvitationCardAggregate());
            queryHandleInvitationRecordRes.setQueryInvitationCardRes(queryInvitationCardRes);
            return queryHandleInvitationRecordRes;
        }).collect(Collectors.toList()));
        return res;
    }


}
